package com.wanghaixin.controller;

import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.ProductWithBLOBs;
import com.wanghaixin.service.ProductCarService;
import com.wanghaixin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/2 20:27
 * @Description: 自有商品模块表现层
 */
@Controller
public class ProductCarController {
    @Autowired
    private ProductCarService productcarService;

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  20:28  2020/11/2
     * @Version:  1.0.0
     * @Description:  分页查询
     */
    @RequestMapping("/sys/product/list")
    //不添加下面注解会报一下错误
    //Error resolving template [sys/make/list], template might not exist or might not be accessible by any of the configured Template Resolvers
    @ResponseBody
    public DataGridResult findByPage(QueryDTO queryDTO){
        return productcarService.findByPage(queryDTO);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  20:28  2020/11/2
     * @Version:  1.0.0
     * @Description:  删除
     */
    @RequestMapping("/sys/product/del")
    @ResponseBody
    public R delProductCar(@RequestBody List<Integer> ids){
        for (Integer id : ids) {
            productcarService.delProductCar(id);
        }
        return R.ok();
    }


    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date: 20:28  2020/11/2
     * @Version:  1.0.0
     * @Description:  id查询，注意发送到页面上的名字
     */
    @RequestMapping("/sys/product/info/{id}")
    @ResponseBody
    public R findById(@PathVariable("id") Integer id){
        //返回值类型发生改变
        ProductWithBLOBs byId = productcarService.findById(id);
        return R.ok().put("product",byId);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date: 20:28  2020/11/2
     * @Version:  1.0.0
     * @Description:  添加方法
     */
    @RequestMapping("/sys/product/save")
    @ResponseBody
    //参数类型发生改变
    public R addProductCar(@RequestBody ProductWithBLOBs productWithBLOBs){
        int i = productcarService.addProductCar(productWithBLOBs);
        return i>0 ? R.ok():R.error("添加失败");
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  20:28  2020/11/2
     * @Version:  1.0.0
     * @Description:  修改方法
     */
    @RequestMapping("/sys/product/update")
    @ResponseBody
    public R upProductCar(@RequestBody ProductWithBLOBs productWithBLOBs){
        int i = productcarService.upProductCar(productWithBLOBs);
        return i>0 ? R.ok():R.error("修改失败");
    }
}
